/*
 * Main_User.c
 *
 * Created  : 6 March 2023
 * Modified : 14 June 2023
 * Author   : HWanyusof
 * Version	: 1.0
 */

#include "usbd_cdc_if.h"
#include "string.h"
#include "stm32f4xx_hal.h"
#include "VEML6046X00.h"
#include "VEML6046X00_Prototypes.h"
#include "typedefinition.h"
#include "VEML6046X00_Application_Library.h"
#include "Main_User.h"


//Global Variables

//Switch the I2C Bus number here (Currently only tested with Teensy3.5 with possibility to use multiple i2c channels)
// I2C_Bus = 1 for Wire 1 (Pin 18-SDA, 19-SCL)
// I2C_Bus = 2 for Wire 2 (Pin 37-SCL, 38-SDA)
//Other Arduino family boards-please use I2C_Bus = 1
//Other platforms will not be affected
int I2C_Bus = 1;


void INIT_VEML6046X00()
{
	/* Reset Sensor to default value */
	Reset_Sensor();

	/****************************************************************************/
	/****************************Basic  Initialization***************************/
	/*
     *Basic_Initialization_Auto_Mode() uses the most basic steps to turn on the RGB Sensor.
     *Very useful when first working with the sensor.
     *For the more advanced settings, you can use either the Auto_Mode() or AF_Mode()
     */

	AF_Mode();

	/***************************End of Initialization****************************/
    /****************************************************************************/
}

//Print Function
void PRINT_VEML6046X00()
{
	/****************************************************************************/
	/*****************************Print Sensor Output****************************/
	/*
	 *Choose Print_Data_Only() to print RGB and IR channel data
	 *To implement the automatic gain control algorithm, choose Print_Data_Only_AGC()
	 */
	Print_Data_Only();
	HAL_Delay(50);
}

//Auto/Self-Timed Mode Basic Initialization Function
void Basic_Initialization_Auto_Mode()
{
	//1.) Switch the sensor on
	VEML6046X00_SET_RGB_ON_0(VEML6046X00_RGB_ON_0_EN);
	VEML6046X00_SET_RGB_ON_1(VEML6046X00_RGB_ON_1_EN);

	//2.) Set the internal calibration after power on
	VEML6046X00_SET_RGB_CAL(VEML6046X00_RGB_CAL_ON);

    //Delay needs to be changed depending on the API of the µ-controller
	HAL_Delay(1000);
}

//AF/Continuous Forced Mode Initialization Function
void AF_Mode()
{
	//1.) Initialization
	//Switch the sensor off
	VEML6046X00_SET_RGB_ON_0(VEML6046X00_RGB_ON_0_DIS);
	VEML6046X00_SET_RGB_ON_1(VEML6046X00_RGB_ON_1_DIS);

	//2.) Setting up RGB
	//RGB_CONF0
	//Set the Integration Time
	VEML6046X00_SET_RGB_IT(VEML6046X00_RGB_IT_3_125ms);
	//Set the measurement mode of the sensor
	VEML6046X00_SET_RGB_MODE(VEML6046X00_RGB_AF_MODE);
	//For AF Mode, Only RGB_AF_DATA_READY is available


	//RGB_CONF1
	//Set the effective photodiode size for the R,G,B and IR channel
	VEML6046X00_SET_PD_DIV(VEML6046X00_PD_DIV_FULL);
	//Set the gain of the RGB
	VEML6046X00_SET_RGB_GAIN(VEML6046X00_RGB_GAIN_x2);
	//Set the amount of consecutive threshold crossing events necessary to trigger the green channel interrupt
	VEML6046X00_SET_RGB_PERS(VEML6046X00_RGB_PERS_1);


	//3.) Switch the sensor on
	VEML6046X00_SET_RGB_ON_0(VEML6046X00_RGB_ON_0_EN);
	VEML6046X00_SET_RGB_ON_1(VEML6046X00_RGB_ON_1_EN);

	//4.) Set the internal calibration after power on
	VEML6046X00_SET_RGB_CAL(VEML6046X00_RGB_CAL_ON);

	//Clear Initial Interrupt
	VEML6046X00_GET_INT_FLAG();

	//Delay needs to be changed depending on the API of the µ-controller
    HAL_Delay(1000);
}

//Auto/Self-Timed Mode Initialization Function
void Auto_Mode()
{
	//1.) Initialization
	//Switch the sensor off
	VEML6046X00_SET_RGB_ON_0(VEML6046X00_RGB_ON_0_DIS);
	VEML6046X00_SET_RGB_ON_1(VEML6046X00_RGB_ON_1_DIS);

	//2.) Setting up RGB
	//RGB_CONF0
	//Set the Integration Time
	VEML6046X00_SET_RGB_IT(VEML6046X00_RGB_IT_25ms);
	//Set the measurement mode of the sensor
	VEML6046X00_SET_RGB_MODE(VEML6046X00_RGB_AUTO_MODE);
	//Enable / disable the green channel interrupt
	VEML6046X00_SET_RGB_INT(VEML6046X00_RGB_INT_EN);

	//RGB_CONF1
	//Set the effective photodiode size for the R,G,B and IR channel
	VEML6046X00_SET_PD_DIV(VEML6046X00_PD_DIV_HALF);
	//Set the gain of the RGB
	VEML6046X00_SET_RGB_GAIN(VEML6046X00_RGB_GAIN_x2);
	//Set the amount of consecutive threshold crossing events necessary to trigger the green channel interrupt
	VEML6046X00_SET_RGB_PERS(VEML6046X00_RGB_PERS_1);

	//RGB_THDH
	//Set the high threshold green channel interrupt value
	VEML6046X00_SET_RGB_HighThreshold(20000);

	//RGB_THDL
	//Set the low threshold green channel interrupt value
	VEML6046X00_SET_RGB_LowThreshold(15000);

	//3.) Switch the sensor on
	VEML6046X00_SET_RGB_ON_0(VEML6046X00_RGB_ON_0_EN);
	VEML6046X00_SET_RGB_ON_1(VEML6046X00_RGB_ON_1_EN);

	//4.) Set the internal calibration after power on
	VEML6046X00_SET_RGB_CAL(VEML6046X00_RGB_CAL_ON);

	//Clear Initial Interrupt
	VEML6046X00_GET_INT_FLAG();

	//Delay needs to be changed depending on the API of the µ-controller
	HAL_Delay(1000);
}

